package com.leet.code.simple_array;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @description:
 * @author: WYG
 * @time: 2020/12/1 19:07
 */

public class AddToArrayForm {

    public List<Integer> addToArrayForm(int[] A, int K) {
        int lenB = 0;
        while(K >= Math.pow(10, lenB)) {
            lenB++;
        }
        int[] B = new int[lenB];
        while(lenB > 0) {
            B[--lenB] = K % 10;
            K = K / 10;
        }
        Integer i = A.length - 1;
        Integer j = B.length - 1;
        int n = i > j ? i : j;
        int[] ansArray = new int[n + 1];
        int num = 0;
        while(i >= 0 && j >= 0) {
            ansArray[n--] = (A[i] + B[j] + num) % 10;
            num = (A[i--] + B[j--] + num) / 10;
        }
        while(i >= 0) {
            ansArray[n--] = (A[i] + num) % 10;
            num = (A[i--] + num) / 10;
        }
        while(j >= 0) {
            ansArray[n--] = (B[j] + num) % 10;
            num = (B[j--] + num) / 10;
        }
        List<Integer> ansList = new ArrayList<Integer>();
        if(num != 0) {
            ansList.add(num);
        }
        for(int val : ansArray) {
            ansList.add(val);
        }
        return ansList;
    }

    public static List<Integer> Form(int[] A, int K) {
        int n = A.length - 1;
        int x = 1;
        int[] KList = new int[10000];
        LinkedList<Integer> res = new LinkedList<>();
        if (K == 0) {
            KList[A.length - 1] = 0;
        }
        int c = -1;
        int j = 9999;
        while (K != 0) {
            int y = K % (x * 10) / x;
            KList[j] = y;
            K = K - y * x;
            x *= 10;
            j--;
            c++;
        }
        j = 9999;
        int temp = 0;
        for (int i = n > c ? n : c; i >= 0; i--) {
            int p = i > n ? 0 : A[i];
            int sum = p + KList[j] + temp;
            System.out.println(p + ":" + sum);
            if (sum < 10) {
                res.addFirst(sum);
                temp = 0;
            } else {
                res.addFirst(sum % 10);
                temp = 1;
            }
            j--;
        }
        if (temp > 0) {
            res.addFirst(temp);
        }
        return res;
    }
    public List<Integer> ToArrayForm(int[] A, int K) {
        int x = 1;
        int[] KList = new int[A.length];
        /**
         455 % 10 / 1 = 5
         455 - 5 * 1 = 450
         450 % 100 / 10 = 5
         450 - 5 * 10 = 400
         450 % 1000 / 100 = 4
         400 - 4 * 100 = 0
         */
        LinkedList<Integer> res = new LinkedList<>();
        if (K == 0) {
            res.add(0);
        }
        int j = A.length - 1;
        while (K != 0) {
            int y = K % (x * 10) / x;
            KList[j] = y;
            K = K - y * x;
            x *= 10;
            j--;
        }
        int temp = 0;
        for (int i = A.length - 1; i >= 0; i--) {
            if (A[i] + KList[i] + temp < 10) {
                res.addFirst(A[i] + KList[i] + temp);
                temp = 0;
            } else {
                res.addFirst((A[i] + KList[i] + temp) % 10);
                temp = (A[i] + KList[i] + temp) / 10;
            }
        }
        if (temp > 0) {
            res.addFirst(temp);
        }
        return res;
    }
}
